package com.zeroxml.cms.web.action.cms;

import com.zeroxml.cms.web.action.BaseAction;
import com.zeroxml.cms.service.ICommentService;
import com.zeroxml.cms.service.IUserService;
import com.zeroxml.cms.service.IArticleService;
import com.zeroxml.cms.bo.Comment;
import com.zeroxml.cms.bo.User;
import com.zeroxml.cms.bo.Article;
import com.zeroxml.cms.exception.ArticleNotExistException;
import com.wyvoid.core.persistent.hibernate.HibernatePage;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.List;
import java.util.Date;

/**
 * User: cjp
 * Date: 2007-9-20
 * Time: 9:56:26
 */
@UrlBinding("/comment.do")
public class CommentAction extends BaseAction {
    @SpringBean
    private ICommentService commentService;
    @SpringBean
    private IArticleService articleService;
    @SpringBean
    private IUserService userService;
    private long articleId;
    private HibernatePage<Comment> comments;
    private List<Comment> latestComments;
    private int curPage;
    private Comment comment;


    public Comment getComment() {
        return comment;
    }

    public void setComment(Comment comment) {
        this.comment = comment;
    }

    public void setArticleId(long articleId) {
        this.articleId = articleId;
    }

    public void setCommentService(ICommentService commentService) {
        this.commentService = commentService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }


    public void setArticleService(IArticleService articleService) {
        this.articleService = articleService;
    }

    public HibernatePage<Comment> getComments() {
        return comments;
    }

    public List<Comment> getLatestComments() {
        return latestComments;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    @DefaultHandler
    public Resolution commentList(){
        int pageSize=20;
        comments=commentService.getComments(articleId,curPage,pageSize);
        return getCmsResolution("comments");
    }

    @HandlesEvent("latest")
    public Resolution latestCommentList(){
        int size=10;
        latestComments=commentService.getLatestComments(size);
        return getCmsResolution("latestComments");
    }

    @HandlesEvent("comment")
    public Resolution commentArticle(){
        long userId=getUserId();
        if(userId<=0){
            userId=1;
        }
        User user=userService.getUser(userId);

        Article article=articleService.getArticle(articleId);
        if(article==null){
            throw new ArticleNotExistException();
        }
        article.setCommentSize(article.getCommentSize()+1);
        articleService.updateArticle(article);
        String ip=getRequest().getRemoteAddr();

        comment.setPostIp(ip);
        comment.setCreator(user);
        comment.setCreateTime(new Date());
        commentService.doComment(comment);
        return new RedirectResolution(getClass());
    }
}
